package com.example.demo.controller;

import com.example.demo.entity.UserEntity;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-06-05
 * Time: 19:56
 */
@RestController
@RequestMapping("/user3")
public class UserController3 {

    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    @Transactional(propagation = Propagation.NESTED)
    public int add(String username, String password) {
        if (username == null || password == null ||
                username.equals("") || password.equals("")) return 0;

        int result = 0;
        UserEntity user = new UserEntity();
        user.setUsername(username);
        user.setPassword(password);
        result = userService.add(user);
        return result;
    }

    @RequestMapping("/test")
    public int test() {
        return 1;
    }
}
